package com.yellowpage.wf.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.dao.BaseDaoHibernateImpl;
import com.yellowpage.white.dao.QueryAssistantSQL;

import com.yellowpage.wf.data.WorkflowPath;

public class WorkflowPathDaoImpl extends BaseDaoHibernateImpl<WorkflowPath, Long> implements WorkflowPathDao {

	public WorkflowPathDaoImpl() {
		super(WorkflowPath.class);
	}		
	
	
	public void copyWorkflowEntity(Long originalWorkflowId, Long dimWorkflowID,Map<String,String> pathNodeMap) {
		String originalWorkflowIdStr=originalWorkflowId.toString();
		List<WorkflowPath> workflowPathlist=this.getObjectListByConditions("from WorkflowPath w where w.workflowId=?",new Object[]{originalWorkflowIdStr});
		for (WorkflowPath workflowPath : workflowPathlist) {
			workflowPath.setWorkflowPathId(null);
			workflowPath.setWorkflowId(dimWorkflowID);
			workflowPath.setNodeFrom(Long.valueOf(pathNodeMap.get(workflowPath.getNodeFrom().toString())));
			workflowPath.setNodeTo(Long.valueOf(pathNodeMap.get(workflowPath.getNodeTo().toString())));
		}
		this.save(workflowPathlist);
	}
	
	public List<Long> getNextNode(Long workflowId, Long workflowNodeId) {
		QueryAssistantSQL sqlAssistant = new QueryAssistantSQL("select NODE_TO  "
						+ " from  wf_workflow_path " + " where 1=1 "
						+ " and workflow_id=" + workflowId.toString()
						+ " and node_from=" + workflowNodeId.toString());
		List<Map> resultList = sqlAssistant.query(this.getConnection(),
				new HashMap());
		List<Long> reustlList = new ArrayList<Long>();
		for (Iterator noteToMapIterator = resultList.iterator(); noteToMapIterator
				.hasNext();) {
			Map noteToMap = (Map) noteToMapIterator.next();
			reustlList.add(Long.valueOf(noteToMap.get("NODE_TO").toString()));
		}

		return reustlList;
	}
	
	public List<Long> getNextNode(Long workflowId, Long workflowNodeId , String displayName) {
	
		String sql = "select NODE_TO  "
			+ " from  wf_workflow_path " + " where 1=1 "
			+ " and workflow_id=" + workflowId.toString()
			+ " and node_from=" + workflowNodeId.toString() + " and display_name = '"+displayName+ "'";
		
		List<Map> resultList = this.query(sql , new HashMap());
		
		List<Long> reustlList = new ArrayList<Long>();
		for (Iterator noteToMapIterator = resultList.iterator(); noteToMapIterator
				.hasNext();) {
			Map noteToMap = (Map) noteToMapIterator.next();
			reustlList.add(Long.valueOf(noteToMap.get("NODE_TO").toString()));
		}

		return reustlList;
	}
		
	public void clearByWorkflowId(List ids) {
		String workflowIds = "";
		if (ids == null || ids.size() < 1)
			return;
		for (Iterator iterator = ids.iterator(); iterator.hasNext();) {
			Long workflowId = (Long) iterator.next();
			workflowIds += "," + workflowId.toString();
		}

		try {

			PreparedStatement ps = this.getConnection().prepareStatement(
					" delete from wf_workflow_path "
							+ " where workflow_id in ( "
							+ workflowIds.substring(1, workflowIds.length())
							+ " ) ");

			ps.execute();
		} catch (SQLException e) {
			this.logger.debug(this, e);
		}
	}
}	